﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace WindowsFormsApplication1
{
    class ProcessValidator : IValidator
    {
        public string validate(string keysDeserialized, string newKey)
        {
            Console.WriteLine("Procesy uzytkownika: " + newKey);
            string[] splitedNewKey = newKey.Split(' ');
            string[] lines = Regex.Split(keysDeserialized, "\r\n");
            int bestMatchDiff = 9999999;
            string bestMatchLogin = "";

            string login = "walidowanyUzytkownik";

            Dictionary<string, int> userProcessesOcc = new Dictionary<string, int>();
            foreach(string process in splitedNewKey)
            {
                if (process.Equals(""))
                    continue;
                //update number of occurences by one
                if (userProcessesOcc.ContainsKey(process))
                {
                    int num;
                    userProcessesOcc.TryGetValue(process, out num);
                    num++;
                    userProcessesOcc.Remove(process);
                    userProcessesOcc.Add(process, num);
                }
                else
                {
                    // add to dictionary 
                    userProcessesOcc.Add(process, 1);
                }
            }

            Console.WriteLine("Czestotliwosc wystepowania:");
            foreach(string process in userProcessesOcc.Keys)
            {
                int num;
                userProcessesOcc.TryGetValue(process, out num);
                Console.WriteLine(process + " : " +  num);
            }


            foreach (string line in lines)
            {
                if (line.Length == 0)   // if empty string then we don't count it
                    continue;

                // Console.WriteLine("Linia: " + line);
                string[] wordsInLine = line.Split(' ');
                string currentLogin = wordsInLine[0].Substring(0, wordsInLine[0].Length - 1);

                Dictionary<string, int> savedUserProcessesOcc = new Dictionary<string, int>();
                foreach (string process in wordsInLine)
                {
                    if (process.Equals("") || process[process.Length-1].Equals(':'))
                        continue;
                    //update number of occurences by one

                    if (savedUserProcessesOcc.ContainsKey(process))
                    {
                        int num;
                        savedUserProcessesOcc.TryGetValue(process, out num);
                        num++;
                        savedUserProcessesOcc.Remove(process);
                        savedUserProcessesOcc.Add(process, num);
                    }
                    else
                    {
                        // add to dictionary 
                        savedUserProcessesOcc.Add(process, 1);
                    }
                }

                int currentDiff = 0;

                // now compare
                Console.WriteLine("Procesy " + line);
                foreach (string process in userProcessesOcc.Keys)
                {
                    if (savedUserProcessesOcc.ContainsKey(process))
                    {
                        int num;
                        savedUserProcessesOcc.TryGetValue(process, out num);
                        int num2;
                        userProcessesOcc.TryGetValue(process, out num2);
                        currentDiff += Math.Abs(num - num2);
                        Console.WriteLine(login + "/" + currentLogin + ") obaj maja uruchomione :  " + process + " , roznica ilosci: " + Math.Abs(num - num2) + ", roznica calkowita : " + currentDiff);     
                    }
                    else
                    {
                        int num2;
                        userProcessesOcc.TryGetValue(process, out num2);
                        currentDiff += num2;
                        Console.WriteLine(login + "/" + currentLogin + ") tylko " + login + " ma uruchomiony:  " + process + " , ilosc : " + num2 + ", roznica calkowita : " + currentDiff);
                    }
                } 
                
                foreach (string process in savedUserProcessesOcc.Keys)
                {
                    if (userProcessesOcc.ContainsKey(process))
                    {
                        continue;
                    }
                    else
                    {
                        int num;
                        savedUserProcessesOcc.TryGetValue(process, out num);
                        currentDiff += num;
                        Console.WriteLine(login + "/" + currentLogin + ") tylko " + login + " ma uruchomiony:  " + process + " , ilosc : " + num + ", roznica calkowita : " + currentDiff);
                    }
                }
                Console.WriteLine(login + "/" + currentLogin + " : calkowita roznica: " + currentDiff);
                if (currentDiff == bestMatchDiff)
                {
                    bestMatchDiff = currentDiff;
                    bestMatchLogin += (" " + wordsInLine[0].Substring(0, wordsInLine[0].Length - 1));
                }
                else if (currentDiff < bestMatchDiff)
                {
                    bestMatchDiff = currentDiff;
                    bestMatchLogin = wordsInLine[0].Substring(0, wordsInLine[0].Length - 1);
                }

            }
            Console.WriteLine("Najmniejsza roznica uruchomionych programow: " + bestMatchDiff + ", uzytkownik: " + bestMatchLogin);
            return bestMatchLogin;
        }

        public Dictionary<string, Tuple<User, int>> compare(User user, List<User> usersList)
        {
            Dictionary<string, Tuple<User, int>> comparison = new Dictionary<string, Tuple<User, int>>();
            return comparison;
        }
    }
}
